package test2;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;

import Tools.math;
import parser.Word;
import parser.cfg;

public class classEvent {
	final static Scanner sc=new Scanner(System.in);
	public static Var getFunc(String classPath,ArrayList<ArrayList<Word>> cs) throws IOException {
		//System.out.println(Tools.greyLang.test_printWord(cs)); 
		if(classPath.equals("class.console.write()")) {
			if(cs.size()!=1) {
				Tools.greyLang.throwErr("类路径class.console.write()要求的参数数量为1", -1);
			}
			
			String result=run.run(cs.get(0),cfg.MODE_VALUE).value;
			System.out.print(result);
			return new Var(false);
		}
		if(classPath.equals("class.console.read()")) {
			if(cs.size()!=0) {
				Tools.greyLang.throwErr("类路径class.console.write()要求的参数数量为0", -1);
			}
			String result=sc.nextLine();
			//System.out.println("返回");
			return new Var("string",result,false);
		}
		if(classPath.equals("class.debug.printVarList()")) {
			System.out.println(Tools.greyLang.test_printVar(Main.varList));
			return new Var(false);
		}
		if(classPath.equals("class.std.toInt()")) {
			if(cs.size()!=1) {
				Tools.greyLang.throwErr("类路径class.std.toInt()要求的参数数量为1", -1);
			}
			String result=run.run(cs.get(0), cfg.MODE_VALUE).value;
			if(!Tools.math.isNum(result)) {
				Tools.greyLang.throwErr("无法将"+result+"转换为整数", -1);
			}
			return new Var("int",result,false);
		}
		if(classPath.equals("class.greyLang.csTest()")) {
			System.out.print("参数数量:"+cs.size());
			return new Var(false);
		}
		if(classPath.equals("class.windows.messageBox()")) {
			if(cs.size()!=2) {
				Tools.greyLang.throwErr("类路径class.windows.messageBox()要求的参数数量为2", -1);
			}
			Var context=run.run(cs.get(0),cfg.MODE_VALUE);
			Var title=run.run(cs.get(1),cfg.MODE_VALUE);
			greyLangCppApi.windowApi.wa.messageBox(title.value,context.value, 0|32);
			return new Var(false);
		}
		
		if(classPath.equals("class.math.isNum()")) {
			if(cs.size()!=1) {
				Tools.greyLang.throwErr("类路径class.math.isNum()要求的参数数量为1", -1);
			}
			Var context=run.run(cs.get(0),cfg.MODE_VALUE);
			if(math.isNum(context.value)) {
				return new Var("bool","true",false);
			}else {
				return new Var("bool","false",false);
			}
		}
		
		if(classPath.equals("class.windows.messageBoxEx()")) {
			if(cs.size()!=4) {
				Tools.greyLang.throwErr("类路径class.windows.messageBoxEx()要求的参数数量为4", -1);
			}
			Var context=run.run(cs.get(0),cfg.MODE_VALUE);
			Var title=run.run(cs.get(1),cfg.MODE_VALUE);
			Var icon=run.run(cs.get(2),cfg.MODE_VALUE);
			Var button=run.run(cs.get(3),cfg.MODE_VALUE);
			if(!icon.type.equals("int")||!button.type.equals("int")) {
				Tools.greyLang.throwErr("类路径class.windows.messageBoxEx()要求的参数3和参数4类型为int", -1);
			}
			int result=greyLangCppApi.windowApi.wa.messageBox(title.value,context.value, Integer.parseInt(button.value)|Integer.parseInt(icon.value));
			return new Var("int",String.valueOf(result),false);
		}
		Tools.greyLang.throwErr("没有找到指定类路径:"+classPath, -1);
		return new Var(false);
	}
	public static Var getSx(String classPath) {
		if(classPath.equals("class.program.path")) {
			return new Var("string",Main.programPath,false);
		}
		if(classPath.equals("class.program.fileName")) {
			return new Var("string",Main.fileName,false);
		}
		if(classPath.equals("class.greyLang.path")) {
			return new Var("string",Main.greyLangPath,false);
		}
		if(classPath.equals("class.windows.messageBox.BUTTON_OK")) {
			return new Var("int",String.valueOf(0x00000000),false);
		}
		if(classPath.equals("class.windows.messageBox.BUTTON_OKCANCEL")) {
			return new Var("int",String.valueOf(0x00000001),false);
		}
		if(classPath.equals("class.windows.messageBox.BUTTON_ABORTRETRYIGNORE")) {
			return new Var("int",String.valueOf(0x00000002),false);
		}
		if(classPath.equals("class.windows.messageBox.BUTTON_YSENOCANCEL")) {
			return new Var("int",String.valueOf(0x00000003),false);
		}
		if(classPath.equals("class.windows.messageBox.BUTTON_YESNO")) {
			return new Var("int",String.valueOf(0x00000004),false);
		}
		if(classPath.equals("class.windows.messageBox.BUTTON_RETRYCANCL")) {
			return new Var("int",String.valueOf(0x00000005),false);
		}
		if(classPath.equals("class.windows.messageBox.ICON_HAND")) {
			return new Var("int",String.valueOf(0x00000010),false);
		}
		if(classPath.equals("class.windows.messageBox.ICON_QUESTION")) {
			return new Var("int",String.valueOf(0x00000020),false);
		}
		if(classPath.equals("class.windows.messageBox.ICON_EXCLAMATION")) {
			return new Var("int",String.valueOf(0x00000030),false);
		}
		if(classPath.equals("class.windows.messageBox.ICON_ASTERISK")) {
			return new Var("int",String.valueOf(0x00000040),false);
		}
		if(classPath.equals("class.windows.messageBox.CLICK_OK")) {
			return new Var("int","1",false);
		}
		if(classPath.equals("class.windows.messageBox.CLICK_CANCEL")) {
			return new Var("int","2",false);
		}
		if(classPath.equals("class.windows.messageBox.CLICK_ABORT")) {
			return new Var("int","3",false);
		}
		if(classPath.equals("class.windows.messageBox.CLICK_RETRY")) {
			return new Var("int","4",false);
		}
		if(classPath.equals("class.windows.messageBox.CLICK_IGNORE")) {
			return new Var("int","5",false);
		}
		if(classPath.equals("class.windows.messageBox.CLICK_YES")) {
			return new Var("int","6",false);
		}
		if(classPath.equals("class.windows.messageBox.CLICK_NO")) {
			return new Var("int","7",false);
		}
		if(classPath.equals("class.windows.messageBox.CLICK_CLOSE")) {
			return new Var("int","8",false);
		}
		if(classPath.equals("class.windows.messageBox.CLICK_HELP")) {
			return new Var("int","9",false);
		}
		if(classPath.equals("class.windows.messageBox.CLICK_TRYAGAIN")) {
			return new Var("int","10",false);
		}
		if(classPath.equals("class.windows.messageBox.CLICK_CONTINUE")) {
			return new Var("int","11",false);
		}
		Tools.greyLang.throwErr("没有找到指定类路径:"+classPath, -1);
		return new Var(false);
	}
	public static Var getFunc(String classPath,ArrayList<ArrayList<Word>> cs,Map<String,ArrayList<Word>> node) throws IOException {
		if(classPath.equals("class.debug.printNode()")) {
			if(cs.size()!=0) {
				Tools.greyLang.throwErr("类路径"+classPath+"要求的参数数量为0", -1);
			}
			for (Map.Entry<String,ArrayList<Word>> entry : node.entrySet()) { 
				System.out.println(entry.getKey());
				System.out.println(Tools.greyLang.test_printWord(entry.getValue()));
			}
			return new Var(false);
		}
		if(classPath.equals("class.std.if()")) {
			if(cs.size()!=1) {
				Tools.greyLang.throwErr("类路径"+classPath+"要求的参数数量为1", -1);
			}
			if(!node.containsKey("main")) {
				Tools.greyLang.throwErr("类路径"+classPath+"调用时必须提供main块节点", -1);
			}
			Var result=run.run(cs.get(0),cfg.MODE_VALUE);
			if(!result.type.equals("bool")) {
				Tools.greyLang.throwErr("类路径"+classPath+"要求的参数1必须为bool类型", -1);
			}
			if(result.value.equals("true")) {
				run.run(node.get("main"), cfg.MODE_COMMON);
			}else {
				if(node.containsKey("else")) {
					run.run(node.get("else"), cfg.MODE_COMMON);
				}
			}
			return new Var(false);
		}
		Tools.greyLang.throwErr("没有找到指定类路径:"+classPath, -1);
		return new Var(false);
	}
}
